/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.member;

import dao.DanhMucDAO;
import dao.SanPhamDAO;
import dao.TinhTrangSanPhamDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Danhmuc;
import pojo.Taikhoan;
import pojo.Tinhtrangsanpham;
import util.UtilCommon;

/**
 *
 * @author Ngoc Linh
 */
@WebServlet(name = "addproduct", urlPatterns = {"/member/addproduct.do"})
public class addproduct extends HttpServlet {
    
    private List<Danhmuc> dsDanhMuc = null;
    private List<Tinhtrangsanpham> dsTinhTrang = null;
    
    @Override
    public void init() {
        dsDanhMuc = DanhMucDAO.LayDanhMucChuaSP();        
        dsTinhTrang = TinhTrangSanPhamDAO.LayDSTinhTrang();
    }

    @Override
    public void destroy() {
        dsDanhMuc = null;        
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if(request.getCharacterEncoding() == null)
                request.setCharacterEncoding("UTF-8");
    
            HttpSession session = request.getSession();
            Taikhoan taiKhoan = null;
            String urlView = "addproduct.jsp";
            
            if(session.getAttribute("taiKhoan") == null){
                session.setAttribute("returnPage", request.getRequestURI()); 
                urlView = "../login.do";
            }else{
                if(request.getParameter("add_new_pro") != null){
                    taiKhoan = (Taikhoan)session.getAttribute("taiKhoan"); 
                    String namePro = request.getParameter("name_pro");
                    int maDM = Integer.parseInt(request.getParameter("cat_pro"));
                    int tinhTrang = Integer.parseInt(request.getParameter("status_pro"));
                    long giaKhoiDiem = Long.parseLong(request.getParameter("price_start_pro"));
                    
                    String strMuaNgay = request.getParameter("price_buynow_pro");
                    long giaMuaNgay = 0;
                    if(strMuaNgay != null && strMuaNgay.trim().length() != 0){
                        giaMuaNgay = Long.parseLong(strMuaNgay);
                    }
                    
                    String strNgayHetHan = request.getParameter("date_stop_pro");
                    Date dateEnd = new Date();
                    dateEnd.setTime(dateEnd.getTime() + 7*60*60*1000); //Thời gian mặc định
                    
                    if(UtilCommon.ConvertToDate(strNgayHetHan) != null){
                        dateEnd = UtilCommon.ConvertToDate1(strNgayHetHan);
                    }
                    
                    String paymentInfo = request.getParameter("payment_shipping_pro");
                    String detailInfo = request.getParameter("detail_info_pro");
                    
                    int maSP = SanPhamDAO.ThemMoi(taiKhoan.getMaTaiKhoan(),namePro,maDM,giaKhoiDiem,giaMuaNgay,dateEnd,paymentInfo,detailInfo,tinhTrang);
                    
                    if(maSP > 0){
                        session.setAttribute("maSPVuaThem", maSP);
                        request.setAttribute("message", "Thêm mới thông tin sản phẩm thành công! Tiếp tục cập nhật hình ảnh cho sản phẩm này !");
                    }else{
                        request.setAttribute("messageError", "Ghi nhận thông tin sản phẩm bạn cung cấp thất bại. Vui lòng thử lại !");
                    }
                }                   
                
                request.setAttribute("danhmuc", dsDanhMuc);
                request.setAttribute("tinhtrang", dsTinhTrang);
            }
            
            RequestDispatcher rd = request.getRequestDispatcher(urlView);
            rd.forward(request, response);
        } catch (Exception ex) {
            response.sendRedirect("../errorpage.do");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
